package com.example.buildtable.controller;

import com.example.common.util.R;
import com.example.buildtable.pojo.TwolevelMenu;
import com.example.buildtable.service.TwolevelMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@Tag(name = "二级菜单")
@RequestMapping(value = "/twolevelMenu/")
public class TwolevelMenuController {
    @Autowired
    private TwolevelMenuService twolevelMenuService;
    
    @Operation(summary = "查询所有二级菜单数据")
    @GetMapping(value = "/getAllTwolevelMenu")
    public R getAllTwolevelMenu() {
        List<TwolevelMenu> twolevelMenuList = twolevelMenuService.list();
        return R.ok("查询所有二级菜单数据成功").data("twolevelMenuList", twolevelMenuList);
    }
    
    @Operation(summary = "增加二级菜单")
    @PostMapping(value = "/addTwolevelMenu")
    public R addTwolevelMenu(TwolevelMenu twolevelMenu) {
        boolean flag = twolevelMenuService.save(twolevelMenu);
        if (flag) {
            return R.ok("增加二级菜单成功");
        } else {
            return R.ok("增加二级菜单失败");
        }
    }

    @Operation(summary = "删除二级菜单")
    @GetMapping(value = "/deleteTwolevelMenu/{twolevelMenuId}")
    public R deleteTwolevelMenu(@PathVariable("twolevelMenuId") Integer twolevelMenuId) {
        boolean flag = twolevelMenuService.removeById(twolevelMenuId);
        if (flag) {
            return R.ok("删除二级菜单成功");
        } else {
            return R.ok("删除二级菜单失败");
        }
    }

    @Operation(summary = "按照二级菜单查看")
    @GetMapping(value = "/getTwolevelMenuById/{twolevelMenuId}")
    public R getTwolevelMenuById(@PathVariable("twolevelMenuId") Integer twolevelMenuId) {
        TwolevelMenu twolevelMenu = twolevelMenuService.getById(twolevelMenuId);
        if (twolevelMenu != null) {
            return R.ok("按照二级菜单查看成功").data("twolevelMenu", twolevelMenu);
        } else {
            return R.error("按照二级菜单查看失败");
        }
    }

    @Operation(summary = "修改二级菜单")
    @PostMapping(value = "/updateTwolevelMenu")
    public R updateTwolevelMenu(TwolevelMenu twolevelMenu) {
        boolean flag = twolevelMenuService.updateById(twolevelMenu);
        if (flag) {
            return R.ok("修改二级菜单成功");
        } else {
            return R.ok("修改二级菜单失败");
        }
    }
}